import {Component, OnDestroy, OnInit} from '@angular/core';
import {SocketService} from '../../services/socket.service';
import {ExtendHttpService} from '../../../common/extend-http.service';
import {TipService} from '../../services/tip.service';

@Component({
  selector: 'app-statistics-list',
  templateUrl: './statistics-list.component.html',
  styleUrls: ['./statistics-list.component.css']
})
export class StatisticsListComponent implements OnInit, OnDestroy {

  unWsMessage: any;

  staticIds: any = [];

  constructor(public sockets: SocketService, private ext: ExtendHttpService, private tips: TipService) {
  }

  ngOnInit() {
    cBack = () => {
      $('#btn-back').trigger('click');
    };

    this.getLoginInfo();

    this.unWsMessage = this.sockets.wsMessage.subscribe(res => {
      console.log(res, 'index组件得到服务器传来的消息');
      this.handleWsMsg(res);
    });
  }

  // 获取登录人信息
  getLoginInfo() {
    this.ext.post('/merchantApp/basics/loginInfo/getInfo', {}).subscribe(res => {
      if (res.json().code === '0') {
        this.staticIds = [];
        let arr = res.json().merchantAppMenu;
        for (let i in arr) {
          this.staticIds.push(arr[i].code);
        }
        // console.log(this.staticIds);
      } else {
        this.tips.change(true, res.json().message);
      }
    });
  }

  // 响应webSocket传来的消息
  handleWsMsg(res) {
    if (res.code === '1001') {
      this.getLoginInfo(); // 重新获取菜单和按钮权限
    }
  }

  ngOnDestroy(): void {
    this.unWsMessage.unsubscribe();
  }

}
